package com.etc.controller;

import com.etc.entity.Scenic;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @Author: 史泽浩
 * @Date: 2019/5/24 16:15
 */

@Controller
public class ScenicController {

    @Autowired
    private RestTemplate rt;

    /**
     * 跳转到景点详情的功能处理方法
     * recommend也要跳到景点详情
     *
     * @param pid
     * @param cid
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "scenicDetails", method = RequestMethod.GET)
    public String toScenicDetails(
            @ModelAttribute("pid") @RequestParam(value = "pid") Integer pid,
            @ModelAttribute("cid") @RequestParam(value = "cid")Integer cid,
            @ModelAttribute("sid") @RequestParam(value = "sid")Integer sid,
            ModelMap modelMap) {
        // 放入rquestScope
        modelMap.put("pid",pid);
        modelMap.put("cid",cid);
        modelMap.put("sid",sid);
        modelMap.put("flag", "area");
        return "scenicDetails";
    }


    @GetMapping("scenics/{sid}")
    @ResponseBody
    public String listScenics(@PathVariable("sid") Integer sid) {
        String result = rt.getForObject("http://apis.haoservice.com/lifeservice/travel/GetScenery?&" +
                "key=b4001a9c860a4f97a8d222e89d405555&sid=" + sid + "&page=&paybyvas=false", String.class);
        return result;
    }

    @GetMapping("cities")
    @ResponseBody
    public String listCities(
            @RequestParam(value = "pid") Integer pid,
            @RequestParam(value = "cid") Integer cid,
            ModelMap modelMap) {
        modelMap.put("flag", "area");
        String result = rt.getForObject("http://apis.haoservice.com/lifeservice/travel/scenery" +
                "?&key=b4001a9c860a4f97a8d222e89d405555&pid=" + pid + "&cid=" + cid + "&page=&paybyvas=false", String.class);
        /*Type scenicsList = new TypeToken<List<Scenic>>(){}.getType();
        List<Scenic> list = new Gson().fromJson(result, scenicsList);
        list.forEach(System.out::println);*/
        return result;
    }
}
